/*
 * $Id: AlertCommand.java 12 2008-01-26 06:42:50Z jasta $
 *
 * Copyright (C) 2008 Josh Guilfoyle <jasta@devtcg.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

package org.devtcg.syncml.protocol;

import org.devtcg.syncml.model.DatabaseMapping;

import org.w3c.dom.*;

public class AlertCommand extends BaseCommand
{
	protected int mCode;
	protected DatabaseMapping mDB;

	public AlertCommand(Element command)
	{
		super("Alert");

		NodeList children = command.getChildNodes();

		for (int i = 0; i < children.getLength(); i++)
		{
			Node n = children.item(i);

			if (n.getNodeName().equals("Data") == true)
				mCode = Integer.valueOf(((Text)n.getFirstChild()).getNodeValue());
			else if (n.getNodeName().equals("CmdID") == true)
				mID = Long.valueOf(((Text)n.getFirstChild()).getNodeValue());
		}
	}

	public AlertCommand(int code, DatabaseMapping db)
	{
		super("Alert");

		mCode = code;
		mDB = db;
	}

	public int getCode()
	{
		return mCode;
	}

	public DatabaseMapping getDB()
	{
		return mDB;
	}

	public String toString()
	{
		StringBuilder b = new StringBuilder();

		b.append("<Alert>\n");
		b.append("<CmdID>").append(getId()).append("</CmdID>\n");
		b.append("<Data>").append(mCode).append("</Data>\n");
		b.append("<Item>\n");
		b.append("<Target><LocURI>").append(mDB.getName()).append("</LocURI></Target>\n");
		b.append("<Source><LocURI>").append(mDB.getName()).append("</LocURI></Source>\n");
		b.append("<Meta>\n");
		b.append("<Anchor>\n");
		b.append("<Last>").append(mDB.getLastAnchor()).append("</Last>\n");
		b.append("<Next>").append(mDB.getNextAnchor()).append("</Next>\n");
		b.append("</Anchor>\n");
		b.append("</Meta>\n");
		b.append("</Item>\n");
		b.append("</Alert>\n");

		return b.toString();
	}
}
